﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Vit.DataAccess;
using Vit.Common.Extensions;

namespace Vit.SchemaExplorer
{
    public class ColumnSchemaInfoRowMapper : IRowMapper<ColumnSchemaInfo>
    {
        #region IRowMapper<ColumnSchemaInfo> Members

        public ColumnSchemaInfo MapRow(IDataRecord row)
        {
            ColumnSchemaInfo obj = new ColumnSchemaInfo();
            obj.SchemaName = row["SchemaName"].ConvertTo<string>(string.Empty);
            obj.OwnerName = row["OwnerName"].ConvertTo<string>(string.Empty);
            obj.ObjectType = row["ObjectType"].ConvertTo<TabularObjectType>(TabularObjectType.Table);
            obj.ObjectName = row["ObjectName"].ConvertTo<string>(string.Empty);
            obj.ObjectDescription = row["ObjectDescription"].ConvertTo<string>(string.Empty);
            obj.ColumnName = row["ColumnName"].ConvertTo<string>(string.Empty);
            obj.ColumntDescription = row["ColumnDescription"].ConvertTo<string>(string.Empty);
            obj.ColumnOrder = row["ColumnOrder"].ConvertTo<int>(0);
            obj.NativeType = row["NativeType"].ConvertTo<string>(string.Empty);
            obj.AllowDBNull = row["AllowDBNull"].ConvertTo<bool>(true);
            obj.Size = row["Size"].ConvertTo<int?>(null);
            obj.Scale = row["Scale"].ConvertTo<int?>(null);
            obj.Precision = row["Precision"].ConvertTo<byte?>(null);
            obj.IsIdentity = row["IsIdentity"].ConvertTo<bool>(false);
            obj.PrimaryKeyName = row["PrimaryKeyName"].ConvertTo<string>(string.Empty);
            obj.PrimaryKeyOrder = row["PrimaryKeyOrder"].ConvertTo<int?>(null);
            obj.ForeignKeyName = row["ForeignKeyName"].ConvertTo<string>(string.Empty);
            obj.ForeignKeySchemaName = row["ForeignKeySchemaName"].ConvertTo<string>(string.Empty);
            obj.ForeignKeyTableName = row["ForeignKeyTableName"].ConvertTo<string>(string.Empty);
            obj.ForeignKeyColumnName = row["ForeignKeyColumnName"].ConvertTo<string>(string.Empty);
            obj.ForeignKeyColumnOrder = row["ForeignKeyColumnOrder"].ConvertTo<string>(string.Empty);

            return obj;
        }

        #endregion
    }
}
